<%@ page language="java" import="java.util.*" pageEncoding="utf-8" contentType="text/html;charset=utf-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Sect Index</title>
 	<%@ include file="/common/view/header.jsp"%>
  	<script>
  		var win=null, form=null, grid=null;
  		$(function(){
  			//create datagrid
			grid = $('#tt').datagrid({
                url:'<%=cp%>/rbac/getAllSection',
                pageSize:20,
    			pageList:[5,10,15,20],
    			loadMsg:'正在加载管理单位列表......',
    			pagination:true,
    			method:"post",
				height: document.body.clientHeight,
				rownumbers:true,
                fitColumns: true,
                singleSelect: true,
                remoteSort:false,
                idField:'id',
                columns:[[
			        {title:'管理单位名称',field:'name',width:fixWidth(0.08),sortable:false,align:'left'},
			        {title:'咨询电话',field:'tel',width:fixWidth(0.08),sortable:false,align:'left'},
                	{title:'说明',field:'intro',width:fixWidth(0.16),sortable:false,align:'left'},
                	{title:'最后更新时间',field:'createTime',width:fixWidth(0.06),sortable:false,align:'center'},
                	{title:'状态',field:'status',width:fixWidth(0.03),sortable:false,align:'center',
                		formatter:function(value,row,index){
                			if(value=="0"){
                				return "<span style='color:#E10900'>已禁用</span>";
                			}
                			return "<span style='color:#009900'>已启用</span>";
                		}
                	},
                	{title:'操作',field:'doaction',align:'center',width:fixWidth(0.05),align:'center',
						formatter:function(value,row,index){
							var e = '<a href="###" onclick="editDataItem('+index+')">编辑</a> ';
							var d = '';
							if(row.status=='0'){
								d = '<a href="###" onclick="setupStatus(\''+index+'\',1)">启用</a>';
							}else if(row.status=='1'){
								d = '<a href="###" onclick="setupStatus(\''+index+'\',0)">禁用</a>';
							}
							return e+d;
						}
					}
				]],
                onDblClickRow:function(rowIndex, rowData){
                	editDataItem(rowIndex);
                },
				toolbar:[{
						id:'btnadd',
						text:'新增单位',
						iconCls:'icon-add',
						handler:newDataItem
					},'-',{
						text:'高级查询',
						iconCls:'icon-search',
						handler:openSearchForm
					},'-',{
						text:'显示全部',
						iconCls:'icon-reload',
						handler:displayAll
					},'-'
				]
            });
			//create window
			init_window();
			//create form
			init_form();
		});
  		/* 初始化窗口控件 */
  		function init_window(){
  			var width = 400;
  			var height = 280;
  			var top = (document.body.clientHeight-height)/2;
  			var left = (document.body.clientWidth-width)/2;
  			win = $('#data_win').window({
				closed:true,
				width: width,
				height: height,
				top: top,
  				left: left,
				modal: true,
				iconCls:'icon-app'
			});
  		}
  		/* 初始化表单控件 */
  		function init_form(){
  			form = win.find('form');
  		}
  		/* 新增数据项 */
  		function newDataItem(){
  			win.window('setTitle','添加单位');
  			win.window('open');
			form.form('clear');
			form.url = '<%=cp%>/rbac/addSection';
  		}
  		/* 编辑数据项 */
  		function editDataItem(index){
  			var row = grid.datagrid('getRows')[index];
  			if (row){
  				win.window('setTitle','更新单位');
				win.window('open');
				form.form('clear');
				form.form('load', '<%=cp%>/rbac/getSection/'+row.id+'?rnd='+Math.random());
				form.url = '<%=cp%>/rbac/editSection/'+row.id+'?rnd='+Math.random();
				
				//以下为 支持ie9需要给form中的select控件赋值
				//$('#group_id').combobox('setValue',row.groupid);
			} else {
				$.messager.show({
					title:'警告',
					msg:'请您先选择单位。'
				});
			}
  		}
  		/* 打开查询表单窗口 */
  		function openSearchForm(){
  			var width = 350;
  			var height = 100;
  			var top = (document.body.clientHeight-height)/2;
  			var left = (document.body.clientWidth-width)/2;
			$('#searchform').window({
				title: '查询',
				width: width,
				height: height,
				top: top,
  				left: left,
				modal: false,
				closed: false,
				minimizable:false,
      			maximizable:false,
      			collapsible:false,
				resizable:false,
				iconCls:'icon-search'
			});
		}
  		/* 关闭表单窗口 */
  		function closeWindow(){
			if(win)
				win.window('close');
		}
  		/* 执行查询 */
  		function do_search(){
			var name = $('#keyword').val();
			grid.datagrid('options').queryParams = {sectionname:name};
			grid.datagrid("options").pageNumber = 1;
			grid.datagrid('getPager').pagination({pageNumber:1});
			grid.datagrid('reload');
		}
  		/* 显示全部记录 */
  		function displayAll(){
  			grid.datagrid('load',{queryname:''});
  		}
  		/* 保存数据 */
  		function do_saveData(){
  			form.form('submit', {
				url:form.url,
				onSubmit:function(){  
					return $(this).form('validate');  
				},
				success:function(data){
					data = unescape(data);
					eval('data='+data);
					if (data.success){
						grid.datagrid('reload');
						closeWindow();
					} else {
						$.messager.alert('错误',data.msg,'error');
					}
				}
			});
  		}
  		/* 设置管理单位状态 */
  		function setupStatus(index,statVal){
  			var row = grid.datagrid('getRows')[index];
  			if(statVal=='0'){
  				stattitle = '禁用，该单位将不能在系统中使用。';
  				fcolor = '#E10900';
  			}else{
  				stattitle = '启用，该单位可继续在系统中使用。';
  				fcolor = '#009900';
  			}
  			$.post(
				'<%=cp%>/rbac/setSectionStatus?rnd='+Math.random(),
				{sectionid:row.id,statVal:statVal},
				function(msg) {
					eval('result='+msg);
					if(result.success==1){
						$.messager.show({
							title:'提示',
							msg:'<span style="color:'+fcolor+';">已成功将<span style="font-weight:bold;">'+row.name+'</span>的状态设置为'+stattitle+'</span>',
							timeout:3000,
							showType:'fade'
						});
						grid.datagrid('reload');
					}else{
						$.messager.alert('错误',result.msg,'error');
					}
				}
			);
  		}
  	</script>
  </head>
  <body style="margin:0px;" id="c_body">
  	<!-- datagrid -->
	<table id="tt"></table>
	<!-- search form -->
	<div id="searchform" class="easyui-window" closed="true" modal="true">
		<table width="100%" cellspacing="1" cellpadding="2" border="0" align="center">
		  <tr>
			<td align="center" height="60">名称：<input type="text" id="keyword"><a class="easyui-linkbutton" icon="icon-search" href="javascript:void(0)" onclick="do_search()">查询</a></td>
		  </tr>
		</table>
	</div>
	<!-- data item form -->
	<div id="data_win" class="easyui-window" closed="true" modal="true">  
	    <div style="text-align:center; padding:1px;">  
	        <form id="data_form" method="post">  
	            <table width="90%" cellspacing="1" cellpadding="2" border="0" align="center">
	                <tr>
	                    <td>单位名称：</td>
	                    <td><input type="text" name="name" size="30" class="easyui-validatebox" required="true" missingMessage="请填写单位名称"></input></td>
	                </tr>
	                <tr>
	                	<td>咨询电话：</td>
	                	<td><input type="text" name="tel" size="30"/></td>
	                </tr>
	                <tr>
	                    <td>备注说明：</td>
	                    <td><textarea name="intro" cols="30" rows="5" style="width:100%;"></textarea></td>
	                </tr>
	            </table>
	        </form>
	    </div>
	    <div style="text-align:center;padding:5px;">
	        <a href="javascript:void(0)" onclick="do_saveData()" id="btn-save" class="easyui-linkbutton" icon="icon-save">保存</a>  
	        <a href="javascript:void(0)" onclick="closeWindow()" id="btn-cancel" class="easyui-linkbutton" icon="icon-cancel">取消</a>  
	    </div>  
	</div>
  </body>
</html>